package org.paterczm.watermarker;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI Builder, which is free for non-commercial use. If Jigloo is being used commercially (ie, by a
 * corporation, company or business for any purpose whatever) then you should purchase a license for each developer using Jigloo. Please visit www.cloudgarden.com for details. Use
 * of Jigloo implies acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR ANY
 * CORPORATE OR COMMERCIAL PURPOSE.
 */
public class WatermarkerGui extends javax.swing.JFrame {
	private JTextField jTextFieldInput;
	private JButton jButtonStart;
	private JButton jButtonOutput;
	private JTextField jTextField1;
	private JTextField jTextFieldOutput;
	private JButton jButtonInput;
	private JComboBox jComboBoxProfiles = new JComboBox();

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		WatermarkerGui inst = new WatermarkerGui();
		inst.setVisible(true);
	}

	public WatermarkerGui() {
		super();
		initGUI();

		loadOptions();

		Profile firstProfile = options.getProfiles().get(0);

		jTextFieldInput.setText(options.getInputDirectory());
		jTextFieldOutput.setText(options.getOutputDirectory());

		for (Profile profile : options.getProfiles())
			jComboBoxProfiles.addItem(profile);

	}

	private Options options;

	public void loadOptions() {
		try {
			options = Options.loadOptions("options.xml");

			if (options.getProfiles().isEmpty()) {
				System.out.println("ERROR: No profiles defined.");
				System.exit(ERROR);
			}
		} catch (FileNotFoundException e) {
			try {
				options = new Options();
				options.saveOptions("options.xml");
			} catch (IOException e1) {
				e1.printStackTrace();
				System.exit(ERROR);
			}
		} catch (IOException e) {
			e.printStackTrace();
			options = new Options();
		}
	}

	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			/*
			 * thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1}; thisLayout.rowHeights = new int[] {7, 7, 7, 7}; thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1,
			 * 0.1}; thisLayout.columnWidths = new int[] {7, 7, 7, 7};
			 */
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				getContentPane().add(jComboBoxProfiles,
						new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
			}
			{
				jTextFieldInput = new JTextField();
				getContentPane().add(jTextFieldInput,
						new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
				jTextFieldInput.setText("jTextField1");
				jTextFieldInput.setSize(295, 25);
				jTextFieldInput.setPreferredSize(new java.awt.Dimension(8, 25));
			}
			{
				jButtonInput = new JButton();
				getContentPane().add(jButtonInput, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
				jButtonInput.setText("Input");
				jButtonInput.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						JFileChooser p = new JFileChooser(".");
						p.setFileSelectionMode(p.FILES_AND_DIRECTORIES);
						if (p.showOpenDialog((new JFrame())) == p.APPROVE_OPTION) {
							jTextFieldInput.setText(p.getSelectedFile().getAbsolutePath());
						}
					}
				});
			}
			{
				jTextFieldOutput = new JTextField();
				getContentPane().add(jTextFieldOutput,
						new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
				jTextFieldOutput.setText("jTextField1");
				jTextFieldOutput.setSize(295, 25);
				jTextFieldOutput.setPreferredSize(new java.awt.Dimension(8, 25));
			}
			{
				jButtonOutput = new JButton();
				getContentPane()
						.add(jButtonOutput, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
				jButtonOutput.setText("Output");
				jButtonOutput.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						JFileChooser p = new JFileChooser(".");
						p.setFileSelectionMode(p.FILES_AND_DIRECTORIES);
						if (p.showSaveDialog((new JFrame())) == p.APPROVE_OPTION) {
							jTextFieldOutput.setText(p.getSelectedFile().getAbsolutePath());
						}
					}
				});
			}
			{
				jButtonStart = new JButton();
				getContentPane().add(jButtonStart, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jButtonStart.setText("Start");
				jButtonStart.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							Watermarker.watermark(getSelectedProfile(), jTextFieldInput.getText(), jTextFieldOutput.getText());

							close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}

			pack();
			this.setSize(400, 207);
			this.setTitle("Watermarker");
			this.setResizable(false);
			this.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent evt) {
					try {
						close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.exit(-1);
					}
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close() throws IOException {
		System.out.println("Closing...");
		System.exit(0);
	}

	private Profile getSelectedProfile() {
		return (Profile) jComboBoxProfiles.getSelectedItem();
	}

}
